import {Component, Input, OnInit} from '@angular/core';
import {SFSchema} from "@delon/form";
import {NzMessageService, NzModalRef, UploadFile} from "ng-zorro-antd";
import {AreaService} from "../../sys-setting/area-manage/area.service";
import {_HttpClient} from "@delon/theme";
import {ApiService} from "@core/services/api";

@Component({
  selector: 'app-partment-edit',
  templateUrl: './partment-edit.component.html',
  styles: []
})
export class PartmentEditComponent implements OnInit {
  @Input()type: number; // 0 新增 1 修改
  @Input()set _param(value) {
    console.log(value)
    if (!value.id) {
      this.param = {
        areaParentName: value.parentName,
        areaParentId: value.areaId,
        areaParentAllId: value.parentAllId + ',' + value.areaId
      }
    }
    if (value.id) {
      this.fetchDetail(value.id)
    }
  }; // 参数
  param: any = {}
  showForm = true;
  cityList: any;
  showDetail = false;
  schema: SFSchema = {
    properties: {
      areaName: {
        type: 'string',
        title: '单元名称',
      },
      areaNumber: {
        type: 'string',
        title: '编号',
        maxLength:2,
        pattern:'[0-9]{2}',
      },
      status: {
        type: 'string',
        title: '状态',
        enum: [
          {label: '启用', value: true},
          {label: '停用', value: false},
        ],
        ui: {
          widget: 'select',
        }
      },
      areaRemark: {
        type: 'string',
        title: '备注',
        ui: {
          widget: 'textarea',
          autoSize: true,
          grid: {span: 24}
        }
      }

    },
    "ui": {
      "spanLabelFixed": 100,
      "grid": {
        "span": 12
      }
    },
    required: ['areaName', 'status']
  }
  submit(data: object): void {
    const method = this.type === 4 ? this.api.area.add.method : this.api.house.update.method;
    const url = this.type === 4 ? this.api.area.add.url : this.api.area.update.url+`/${data['id']}`;
    data['areaParentId'] = data['areaParentId'];
    data['areaParentAllId'] = data['areaParentAllId'];
    data['areaRank'] = 9;
    data['policeId'] = '0';
    this.http.request<any>(method, url, {body:data}).subscribe(res => {
      if (res.status === '201' || res.status === '200') {
        this.msg.success(res.message);
        this.modalRef.triggerOk();
      } else {
        this.msg.error(res.message)
      }
    })
  }
  fetchDetail(id) {
    this.http.get(this.api.area.getInfoById.url + `/${id}`, ).subscribe(res => {
      this.param = res.data;
      this.showDetail = true;
    })
  }
  feachCommunity(): void {
    this.http.post(this.api.area.list.url, {areaName: ''}).subscribe((res: any) => {
      if (res.status == '200') {
        this.schema.properties.areaId.enum = res.data.map(item => {
          return {
            label: item.areaName,
            value: item.id
          }
        });
        this.showForm = true;
      }
    })
  }
  constructor(
    private msg: NzMessageService,
    private modalRef: NzModalRef,
    private area: AreaService,
    private http: _HttpClient,
    private api: ApiService
  ) { }
  ngOnInit() {
    // this.feachCommunity();
  }


}
